package com.example.administrator.weipiao;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

import java.io.File;

import loadvideo.DownloadService;
import loadvideo.SDUtil;

public class VideoActivity extends AppCompatActivity {
    private VideoView vv;
    private String fileName;
    private MyReceiver receiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);
        vv = (VideoView) findViewById(R.id.vv_play);
//        注册接收器
        receiver = new MyReceiver();
        IntentFilter filter=new IntentFilter();
        filter.addAction("receiver");
        registerReceiver(receiver,filter);

        Log.i("tag","onCreate");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);
    }

    //  下载视频
    public void download(View view) {
        startService(new Intent(this, DownloadService.class));
    }
    //播放视频
    public void start(View view) {
//      路径

        Uri uri=Uri.parse(SDUtil.getSDPath()+ File.separator+"1607"+File.separator+fileName);
//        设置播放文件路径
        vv.setVideoURI(uri);
//        设置进度条
        vv.setMediaController(new MediaController(this));
        vv.start();
    }
    class MyReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if("receiver".equals(action)){
                fileName = intent.getStringExtra("key");
                Toast.makeText(VideoActivity.this, "下载成功", Toast.LENGTH_LONG).show();
            }
        }
    }
}
